package com.uobgroup.ews.uam.core.dao;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.uobgroup.ews.uam.core.common.UMMConstants;
import com.uobgroup.ews.uam.core.configuration.AppConfig;
import com.uobgroup.ews.uam.core.domain.Reference;
import com.uobgroup.ews.uam.core.domain.SecurityGroup;
import com.uobgroup.ews.uam.core.domain.UAMRole;

@Repository
public class UAMRoleDaoImpl extends AbstractJpaDao<UAMRole> implements UAMRoleDao {
   
   public UAMRoleDaoImpl() {
      setClazz(UAMRole.class);
   }
   
   @Override
   public long count() {
      return entityManager.createQuery("SELECT COUNT(o) FROM UAMRole o", Long.class).getSingleResult();
   }

   @Override
   public List<UAMRole> findAll(int firstResult, int maxResults) {
      return entityManager.createQuery("SELECT o FROM UAMRole o", UAMRole.class)
            .setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
   }

   @SuppressWarnings("unchecked")
   @Override
   public List<UAMRole> findByName(String name) {
      String queryStr = "";
      if(name == null){
         queryStr = "select r from UAMRole r where r.name is null";
      } else{
         queryStr = "select r from UAMRole r where lower(r.name) like :name";
      }
      Query query = entityManager.createQuery(queryStr);
      if(name != null){
         query.setParameter("name", "%" + name.toLowerCase() + "%");
      }
      return query.getResultList();
   }
   @Override
   public Boolean IsAssignProfile(Long RoleId)
   {
	   List<?> list = entityManager.createNativeQuery("SELECT * " +
	   														"FROM PROFILEROLES " +
	   														"WHERE ROLES="+RoleId).getResultList();
	   if(list.size() > 0)
		   return true;
	   else 
		   return false;
   }
   
   @Override
   public Boolean IsExistSecurityGroup(UAMRole uamRole)
   {
	   String DN = AppConfig.getProperty(UMMConstants.LDAP_DN_GROUP_PREFIX + uamRole.getParentGroupName());
	   List<SecurityGroup> list = entityManager.createQuery("select s from SecurityGroup s where s.displayName='"+uamRole.getName()+"' and s.DN='"+DN+"'", SecurityGroup.class)
			   .getResultList();
	   if(list.size() > 0)
		   return true;
	   else
		   return false;
	   
	   
   }
   /**
    * @author dongn
    */
   @Override
   public long countRoleThatHasRoleTypeReference(Reference roleType) {
      return entityManager.createQuery("SELECT COUNT(R) FROM UAMRole R WHERE R.roleType = :roleType", Long.class)
            .setParameter("roleType", roleType).getSingleResult();
   }

   @Override
   public long countRoleThatHasProcessingCentreReference(Reference processingCentre) {
      return entityManager.createQuery("SELECT COUNT(R) FROM UAMRole R WHERE R.processingCentre = :processingCentre", Long.class)
            .setParameter("processingCentre", processingCentre).getSingleResult();
   }

   @Override
   public long countRoleThatHasDepartmentReference(Reference department) {
      return entityManager.createQuery("SELECT COUNT(R) FROM UAMRole R WHERE R.department = :department", Long.class)
            .setParameter("department", department).getSingleResult();
   }
}
